package com.luculent.conf;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Lists;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@Configuration
public class Swagger2Config {
 
    @Bean
    public Docket createRestApi() {
    	Predicate<RequestHandler> selector1 = RequestHandlerSelectors.basePackage("com.luculent.api");
         
        return new Docket(DocumentationType.SWAGGER_2)
        		 
        		.apiInfo(apiInfo()).select()
                // 为当前包路径
                //.apis(RequestHandlerSelectors.any())
        		//.apis(Predicates.or(selector1,selector2))
        		.apis(Predicates.or(selector1))
        		
                .paths(PathSelectors.any()).build().securityContexts(securityContexts())
                .securitySchemes(securitySchemes());
    }

    // 构建 api文档的详细信息函数
    private ApiInfo apiInfo() {
    	 
        return new ApiInfoBuilder()
                // 页面标题
                .title("AegeanSea")
                // 创建人 
                .contact(new Contact("Luculent", "http://www.luculent.net/", "neimenggu@luculent.net"))
                // 版本号
                .version("1.0")
                // 描述
                .description("数据接口").build();
    }
    
    private List<ApiKey> securitySchemes() {
    	 
        return Lists.newArrayList(new ApiKey("iwApikey", "iw-apikey", "header"));
     
      }
     
      private List<SecurityContext> securityContexts() {
     
        SecurityContext context = SecurityContext.builder()
            .securityReferences(defaultAuth())
            //.forPaths(PathSelectors.regex("^(?!auth).*$"))
            .build();
     
        return Lists.newArrayList(context);
     
      }
     
      private List<SecurityReference> defaultAuth() {
   
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");
     
        return Lists.newArrayList(new SecurityReference("iw-apikey", authorizationScopes));
     
      }

}